package com.hall.controller;

import com.hall.pojo.CommentData;
import com.hall.pojo.SysUser;
import com.hall.service.CommentServiceImpl;
import com.hall.service.HallService;
import com.hall.service.HallServiceImpl;
import com.hall.service.UserServiceImpl;
import com.mysql.jdbc.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
/*
* */
@Controller
@RequestMapping(value ="/comment")
public class CommentController {
    @Autowired
    private CommentServiceImpl commentService;
    @Autowired
    private UserServiceImpl userService;
    @ModelAttribute
    public CommentData get(@RequestParam(required=false) String id) {
        CommentData entity = null;
        if (!StringUtils.isNullOrEmpty(id)){
            entity = commentService.findCommentById(id);
        }
        if (entity == null){
            entity = new CommentData();
        }
        return entity;
    }
    /*需要参数：会议appointmentId(显示会议信息)、userId(评价人，当前登录用户)、*/
    @RequestMapping(value = "list")
    public String Commenlist(HttpServletRequest request,CommentData commentData,Model model){
        //获取Boke项目中当前登录用户
        HttpSession sessionB = request.getSession();
        ServletContext ContextB = sessionB.getServletContext();
        HttpSession session=(HttpSession)ContextB.getContext("/boke").getAttribute("session");
        if(session!=null){
            String userId = (String) session.getAttribute("userId");
            SysUser sysUser = userService.getUser(userId);
            model.addAttribute("sysUser",sysUser);
        }
        List<CommentData>commentDataList=commentService.findCommentList(commentData);
        model.addAttribute("commentlist",commentDataList);
        return "commentlist";
    }
    @RequestMapping(value = "save")
    @ResponseBody
    public String saveComment(CommentData commentData,Model model){
        commentData.setCreateDate(new Date());
        commentService.saveComment(commentData);
        return "ok";
    }
    @RequestMapping("islogin")
    @ResponseBody
    public String isLogin(HttpServletRequest request){
        HttpSession session=request.getSession(false);
        SysUser sysUser= (SysUser) session.getAttribute("logined_user");
        if(session!=null && sysUser!=null){
          return "OK";
        }
        return "NO";
    }
       @RequestMapping("share")
      public String share(){
        return "redirect:http://www.baidu.com";
       }
}
